// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import Tool from "./Tool";
import value = cc.js.value;

const {ccclass, property} = cc._decorator;
export  enum soundList  {
    levelBG = "bg.mp3",
    musicMenu = "music_menu.mp3",
    meetEvil = "meet_evil.mp3",
    uiClick = "ui_button.mp3",
    victory = "sword_attack.mp3",
    die = "player_die.mp3",
    walk = "footsteps_loop.mp3",
    jump = "player_jump_01.mp3",
    landFloor = "player_land1.mp3",
    landStone= "player_land3.mp3",
    pickTool = "note_happy.mp3",
    stoneFall = "stone_box_fall.mp3",
    pushBox = "wood_box_move.mp3",
    boxLand = "wood_box_fall.mp3",
    openSwitch = "snap.mp3",
    wallBreak = "destroy_wall.mp3",
    liftDoor = "spike_attack.mp3",
    openlock = "lock_open.mp3",
    pushDoor = "portal_loop.mp3",
    fireBurn = "fire_loop.mp3",
    water = "water_loop.mp3",
    moveFast = "moving_spikes_platform.mp3",
    moveSlow = "platform_slow_move_loop",
    arrowMove = "falling_spikes_fast.mp3",
    switchRoom = "switch_room.mp3",
    whiteEffect = "bottle.mp3",
    whiteEffect2 = "bottle_burst.mp3",
    buttonPress = "button.mp3",
    colour = "colour.mp3",
    enterLevel1 = "audio1.mp3",   //好自为之
    enterLevel2 = "audio2.mp3",   //有备而来
    enterLevel3 = "audio3.mp3",   //以和为贵
    dieLevel1 = "dieMusic1.mp3",     //年轻人不讲武德
    dieLevel2 = "dieMusic2.mp3",     //大意了，没有闪
    dieLevel3 = "dieMusic3.mp3",     //对不起，我不懂规矩
    dieLevel4 = "dieMusic4.mp3",     //蹭了一下，但没关系
    dieLevel5 = "dieMusic5.mp3",     //以后不要再犯
    dieLevel6 = "dieMusic6.mp3",      //好好反思
    vortex = "vortex.mp3",
    fireBurn1 = "fireburn.mp3",
    lockOpen = "lockopen.mp3",
    fireDie = "firedie.mp3",
    chicken = "jizai.mp3",
    rewardBG = "reward.mp3",
    spin = "spin.mp3",
    duckBgm = "duckbgm.mp3",
    duckVoice1 = "ga.mp3", //噶
    duckVoice2 = "meizhuadao.mp3", //没抓到
    duckVoice3 = "zhuadaole.mp3", //抓到了
    duckVoice4 = "zhuajizhi.mp3", //抓几只
    duckVoice5 = "zhuasizhi.mp3", //抓四只
    duckVoice6 = "zhuayazi.mp3", //抓鸭子
}

@ccclass
export default class AudioManage  {

    static bgmVolume:number = 1;
    static sfxVolume:number = 1;
    static bgmAudioID:number = -1;
    static downMusicID : number = -1;
    static _musicOpen:boolean = true;
    static _sfxOpen:boolean = true;
    static audioList = {};
    static downMusic = null;
    static isPlayMusic = false;
    static curVolume = false;

    static audioIDList  = [];
    static audioActionList = {};

    static init () {
        var b1 = Tool.getData("_musicOpen");
        var b2 = Tool.getData("_sfxOpen");
        this._musicOpen = (b1 == "1" || b1 == null|| b1 == "")?true:false;
        this._sfxOpen = (b2 == "1"|| b2 == null|| b2 == "")?true:false;

    }
    static getUrl(url){
        return cc.url.raw("resources/sound/" + url);
    }

    static playBGM(url){
        if(this._musicOpen == false) return;
        console.log(this.bgmVolume);
        let name = url.split(".mp3");
        let realUrl = name[0];
        cc.loader.loadRes("sound/" + realUrl,cc.AudioClip,(error, resource) => {
            if(error){
                console.log(error);
                return;
            }
            this.bgmAudioID = cc.audioEngine.play(resource,true,this.bgmVolume);
            console.log(this.bgmAudioID);
        });
    }

    static  setBgmVolume(value){
        this.bgmVolume = value;
        cc.audioEngine.setVolume(this.bgmAudioID,value);
    }

    static initBgmVolume(){
        cc.audioEngine.setVolume(this.bgmAudioID,this.bgmVolume);
    }

    static setVoiceVolume(value){
        this.sfxVolume = value;
    }

    static  stopBGM(){
        cc.audioEngine.setVolume(this.bgmAudioID,0);
    }

    static resumeBGM(){
        cc.audioEngine.setVolume(this.bgmAudioID,this.bgmVolume);
    }

    static playLevelMusic(url,close = false){
        if(close){
            this.playSFX(url,true,true,1);
            this.isPlayMusic = false;
        }else{
            if(!this.isPlayMusic){
                this.playSFX(url,true,false,1);
                this.isPlayMusic = true;
            }
        }
    }

    static playSFX(url,loop = false,close = false,closeOrder = null){
        if(this._sfxOpen == false) return;
        if(close){
            if(this.audioActionList[url + closeOrder] == null || this.audioActionList[url + closeOrder] == undefined){
                console.log("该语音未调用过")
            }else{
                cc.audioEngine.stop(this.audioActionList[url + closeOrder]);
                this.audioActionList[url + closeOrder] = null;
            }
            return;
        }
        if(this.sfxVolume > 0){
            let name = url.split(".mp3");
            if(name == null){
                name = url.split(".wav");
            }
            let realUrl = name[0];
            if(this.audioList[realUrl] == undefined){
                cc.loader.loadRes("sound/" + realUrl,cc.AudioClip,(error, resource) => {
                    if(error){
                        console.log(error);
                        return;
                    }
                    let action = cc.audioEngine.play(resource,loop,this.sfxVolume);
                    this.audioList[realUrl] = resource;
                    if(closeOrder != null){
                        this.audioActionList[url + closeOrder] = action;
                    }
                })
            }else{
                let audioRes = this.audioList[realUrl];
                let action = cc.audioEngine.play(audioRes,loop,this.sfxVolume);
                if(closeOrder != null){
                    this.audioActionList[url + closeOrder] = action;
                }
            }
        }
    }

    static pauseAll(){
        cc.audioEngine.pauseAll();
    }

    static stopAll(){
        this.pauseAll();
        this._sfxOpen = false;
    }

    static replayAll(){
        this.resumeAll();
        this._sfxOpen = true;
    }

    static targetPlayMusic(url){
        if(this._sfxOpen == false){return};
        if(this.sfxVolume > 0){
            if(this.downMusic != null){
                cc.audioEngine.play(this.downMusic,false,0.4);
            }else{
                let name = url.split(".mp3");
                let realUrl = name[0];
                cc.loader.loadRes("sound/" + realUrl,cc.AudioClip,(error, resource) => {
                    if(error){
                        console.log(error);
                        return;
                    }
                    this.downMusicID = cc.audioEngine.play(resource,false,0.4);
                    this.downMusic = resource;
                })
            }
        }
    }

    static targetStopMusic(){
        cc.audioEngine.stop(this.downMusicID);
    }


    static resumeAll(){
        cc.audioEngine.resumeAll();
    }
    static sFXSwitch(is){
        this._sfxOpen = is;
        Tool.saveData("_sfxOpen",is?1:0);
    }

    static mucSwitch(is){
        this._musicOpen = is;
        if(is ){
            this.playBGM(soundList.bgm);
        }else{
            cc.audioEngine.stopAll();
        }
        Tool.saveData("_musicOpen",is?1:0);

    }

    static setSoundVolume (v) {
        this.sfxVolume = v;
        cc.audioEngine.setVolume(this.bgmAudioID, v);
    }


    // update (dt) {}
}
